/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business.Mail;

/**
 *
 * @author Domenico
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtility
{
  public void sendMail (String dest, String mitt, String oggetto, String testoEmail)
      throws MessagingException
  {
    String  d_email = "email@gmail.com",
        d_uname = "email",
        d_password = "password",
        d_host = "smtp.gmail.com",
        d_port  = "465", //465,587
        m_to = "colletta.domenico@gmail.com",
        m_subject = "Testing",
        m_text = "Hey, this is the testing email.";

        Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
        SMTPAuthenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
session.setDebug(true);
        MimeMessage msg = new MimeMessage(session);
        
        //msg.setText(m_text);
        
        
         //
        // This HTML mail have to 2 part, the BODY and the embedded image
        //
        
        MimeMultipart multipart = new MimeMultipart("related");

        // first part  (the html)
        BodyPart messageBodyPart = new MimeBodyPart();
        String testo = "";
        try {
            RandomAccessFile f = new RandomAccessFile("C:\\ordine.html", "r");
            byte[] fileb = new byte[(int)f.length()];
            f.read(fileb);
            
            testo = new String(fileb);
            f.close();
            
        } catch (IOException ex) {
            Logger.getLogger(MailUtility.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        String htmlText = testo;
        //messageBodyPart.setContent(htmlText, "text/html");
        msg.setContent(htmlText, "text/html; charset=utf-8");
        // add it
        //multipart.addBodyPart(messageBodyPart);
/*        
        // second part (the image) ATTACHEMNT
        messageBodyPart = new MimeBodyPart();
        DataSource fds = new FileDataSource
          ("C:\\fattura\\logo.png");
        messageBodyPart.setDataHandler(new DataHandler(fds));
        messageBodyPart.setHeader("Content-ID","<\\fattura\\logo.png>");
        messageBodyPart.setFileName("\\fattura\\logo.png");
        // add it
        multipart.addBodyPart(messageBodyPart);
*/
        // put everything together
        //msg.setContent(multipart);

        
        
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

Transport transport = session.getTransport("smtps");
transport.connect(d_host, 465, d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
  }
  private class SMTPAuthenticator extends Authenticator
{
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("colletta.domenico@gmail.com", "abraxas55");
        }
};
}
